/**
 * Created by 都大爽 on 2017/8/7.
 */
import Vue from 'vue';
import Vuex from 'vuex';
import selectData from '../api/admin/getSelectList';
import Cookie from 'js-cookie';
Vue.use(Vuex);

const debug = process.env.NODE_ENV !== 'production';

export default new Vuex.Store({
  state: {
    /**
     * 隐藏目录导航
     */
    hideMenuText: false,
    /**
     * 显示的标签列表
     */
    pageOpenedList: [
      {
        path: '/home/index',
        name: 'home_index',
        title: '首页'
      }
    ],

    /**
     * 页面标题
     */
    title: '',

    /**
     * 登录后跳转地址
     */
    authRedirectUrl: '/',

    /**
     * 用户信息
     */
    user: {
      name: '',
      email: '',
      mobile: '',
      avatar: '',
      permissions: [], //权限列表
      can: function (permission) {
        return _.indexOf(this.permissions, permission) > -1;
      }
    },

    /**
     * 面包屑
     */
    breadCrumbs: [],
    /**
     * 组织机构列表
     */
    organizations:[],
    mediators:[],
    permissions:[],
  },
  getters: {

  },

  mutations: {
    /**
     * 显示或隐藏左侧导航
     * @param state
     */
    changeHideMenuText (state) {
      state.hideMenuText = !state.hideMenuText
    },

    /**
     * 添加一个新的标签页
     * @param state
     * @param route
     */
    openNewPage (state, route) {
      let pageIndex = _.findIndex(state.pageOpenedList, ['name', route.name]);
      if (pageIndex === -1) {
        state.pageOpenedList.push({
          path: route.path,
          name: route.name,
          title: route.meta.title,
          params: route.params,
          query: route.query
        });
      } else {
        state.pageOpenedList[pageIndex].params = route.params;
        state.pageOpenedList[pageIndex].query = route.query;
      }
    },

    /**
     * 移除一个标签页
     * @param state
     * @param name
     */
    removeTag (state, name) {
      state.pageOpenedList.map((item, index) => {
        if (item.name === name) {
          state.pageOpenedList.splice(index, 1);
        }
      });
    },

    /**
     * 清空所有标签页
     * @param state
     */
    clearAllTags (state) {
      state.pageOpenedList.splice(1);
    },

    /**
     * 清空其他标签页
     * @param state
     * @param name
     */
    clearOtherTags (state, name) {
      if (name === 'home_index') {
        state.pageOpenedList.splice(1);
      } else {
        let currentTag = _.find(state.pageOpenedList, ['name', name]);
        state.pageOpenedList = [
          {
            path: '/home/index',
            name: 'home_index',
            title: '首页'
          },
          currentTag
        ]
      }
    },

    /**
     * 变更标题
     * @param state
     * @param title
     */
    setTitle (state, title) {
      state.title = title;
      document.title = '人民调节管理信息系统-' + state.title;
    },

    /**
     * 设置登录后重定向地址
     * @param state
     * @param authRedirectUrl
     */
    setAuthRedirectUrl (state, authRedirectUrl) {
      state.authRedirectUrl = authRedirectUrl;
    },

    /**
     * 设置当前用户
     * @param state
     * @param user
     */
    setUser(state, user){
      // debug && console.log(user);
      state.user.id = user.id;
      state.user.name = user.name;
      state.user.email = user.email;
      state.user.mobile = user.mobile;
      state.user.permissions = user.permissions;
    },

    /**
     * 设置面包屑
     * @param state
     * @param breadCrumbs
     */
    setBreadCrumbs(state, breadCrumbs){
      debug && console.log(breadCrumbs);
      state.breadCrumbs = breadCrumbs;
    },

    /**
     * 根据路由设置面包屑
     * @param state
     * @param route
     */
    setBreadCrumbsByRoute (state, route) {
      if (route.meta.breadCrumbs) {
        state.breadCrumbs = route.meta.breadCrumbs;
      }
    },
    /**
     * 设置组织列表
     * @param state
     * @param route
     */
    setOrganization(state, organization) {
        state.organizations = organization;
    },
    setMediators(state, mediator) {
       state.mediators = mediator
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
    }
  },

  actions: {

    /**
     * 获取当前用户
     * @param commit
     */
    fetchUser({commit}){
      axios.get('/api/admin/profile', {params: {include: 'profile'}}).then(function (response) {
        debug && console.log('Susscee:', response);
        let user = response.data.data;
        commit('setUser', user);
        Cookie.set('user', {
          name: user.name,
          email: user.email,
          mobile: user.mobile,
          permissions: user.permissions
        });
      }).catch(function (error) {
        // Something happened in setting up the request that triggered an Error
        debug && console.log('Error', error.message);
      });
    },

    /**
     * 获取组织列表
     * @param commit
     * @param state
     */
    fetchOrganizationList({commit, state}){
      if (state.organizations.length < 1) {
        selectData.getOrganizations( function (data) {
          commit('setOrganization', data);
        });
      }
    },
    /**
     * 调解员列表
     * mediator-list
     */

    fetchMediatorList({commit, state}){
      if (state.mediators.length < 1) {
        selectData.getMediators( function (data) {
          commit('setMediators', data);
        });
      }
    },
    fetchPermissionList({commit, state}){
      if (state.permissions.length < 1) {
        selectData.getPermissions( function (data) {
          commit('setPermissions', data);
        });
      }
    },
  },
  modules: {
    // member
    // award,
    // cms
  },

  strict: debug,
  // plugins: debug ? [createLogger()] : []
})
